from fastapi import Request
from sqlalchemy.orm import Session

from core.dao.cms_gaode_terminal_dao import CmsGaodeTerminalDao
from core.entity.vo.common import CrudResponse
from core.entity.vo.view_models import CmsGaodeTerminalVO
from core.service.gaode.gaode_terminal_service import GaodeTerminalService


class CmsGaodeTerminalService:
    """
    高德终端service
    """

    @classmethod
    def get_cms_gaode_terminal_by_id(cls, result_db: Session, id: int):
        """
        获取参数配置列表信息service
        :param result_db: orm对象
        :param id: id
        :return: 参数配置列表信息对象
        """
        cms_gaode_terminal = CmsGaodeTerminalDao.get_cms_gaode_terminal_by_id(result_db, id)

        return cms_gaode_terminal

    @classmethod
    def get_cms_gaode_terminal_by_terminal_id(cls, result_db: Session, terminal_id: str, start_time: str=None,
                                             end_time: str=None):
        """
        获取参数配置列表信息service
        :param result_db: orm对象
        :param terminal_id: 高德终端id
        :return: 参数配置列表信息对象
        """
        cms_gaode_terminal = CmsGaodeTerminalDao.get_cms_gaode_terminal_by_terminal_id(result_db, terminal_id, start_time, end_time)
        return cms_gaode_terminal
    @classmethod
    def get_cms_gaode_terminal_like_name(cls, result_db: Session, terminal_name: str):
        """
        获取参数配置列表信息service
        :param result_db: orm对象
        :param query_object: 查询参数对象
        :return: 参数配置列表信息对象
        """
        cms_gaode_terminal_list = CmsGaodeTerminalDao.get_cms_gaode_terminal_like_name(result_db, terminal_name)

        return cms_gaode_terminal_list

    @classmethod
    async def add_cms_gaode_terminal(cls, request: Request, result_db: Session, cms_gaode_terminal: CmsGaodeTerminalVO):
        """
        新增参数配置信息service
        :param request: Request对象
        :param result_db: orm对象
        :param cms_gaode_terminal: 新增参数配置对象
        :return: 新增参数配置校验结果
        """

        try:
            result = GaodeTerminalService.add_terminal(cms_gaode_terminal.service_id,
                                                       cms_gaode_terminal.terminal_name,
                                                       cms_gaode_terminal.terminal_desc)
            if result['errcode'] == 10000:
                cms_gaode_terminal.terminal_id = result['data']['tid']
                CmsGaodeTerminalDao.add_cms_gaode_terminal_dao(result_db, cms_gaode_terminal)
                result_db.commit()
                result = dict(is_success=True, message='新增成功')
            else:
                result = dict(is_success=False, message=str(result['errmsg']))
        except Exception as e:
            result_db.rollback()
            result = dict(is_success=False, message=str(e))
        return CrudResponse(**result)

    @classmethod
    async def edit_cms_gaode_terminal(cls, request: Request, result_db: Session, cms_gaode_terminal: CmsGaodeTerminalVO):
        """
        编辑参数配置信息service
        :param request: Request对象
        :param result_db: orm对象
        :param cms_gaode_terminal: 编辑参数配置对象
        :return: 编辑参数配置校验结果
        """
        result = GaodeTerminalService.update_terminal(cms_gaode_terminal.service_id, cms_gaode_terminal.terminal_id,
                                                      cms_gaode_terminal.terminal_name, cms_gaode_terminal.terminal_desc)
        if result['errcode'] == 10000:
            try:
                edit_post = cms_gaode_terminal.dict(exclude_unset=True)
                CmsGaodeTerminalDao.edit_cms_gaode_terminal_dao(result_db, edit_post)
                result_db.commit()
                result = dict(is_success=True, message='修改成功')
            except Exception as e:
                result_db.rollback()
                result = dict(is_success=False, message=str(e))
        else:
            result = dict(is_success=False, message=str(result['errmsg']))
        return CrudResponse(**result)

    @classmethod
    def delete_cms_gaode_terminal(cls, result_db: Session, id: int, service_id: str, terminal_id: str):
        """
        删除菜单信息service
        :param result_db: orm对象
        :param id: id
        :param service_id: 服务编码
        :param terminal_id: 终端编码
        :return: 删除终端
        """
        if id > 0:
            result = GaodeTerminalService.delete_terminal(service_id, terminal_id)
            if result['errcode'] == 10000:
                try:
                    CmsGaodeTerminalDao.delete_cms_gaode_terminal_dao(result_db, id)
                    result_db.commit()
                    result = dict(is_success=True, message='删除成功')
                except Exception as e:
                    result_db.rollback()
                    result = dict(is_success=False, message=str(e))
            else:
                result = dict(is_success=False, message=str(result['errmsg']))
        else:
            result = dict(is_success=False, message='传入id为空')
        return CrudResponse(**result)

